<?php

namespace app\common\socket;

use liketp\facade\Timer;
use liketp\socket\EventsHandle;

class Events extends EventsHandle
{
    public static function onConnect($client)
    {
        self::sendToClient($client, 'welcome ' . $client);
    }

    public static function onMessage($client, $msg)
    {
        $response = 'You say: ' . $msg;
        self::sendToClient($client, $response);
        $count = ['n' => 0, 'timerId' => -1];
        $timer = Timer::add(1, function ($k, $a, $l) use ($client, &$count) {
            self::sendToClient($client, 'timer, the args is: ' . implode(',', compact('k', 'a', 'l')));
            $count['n']++;
            if ($count['n'] > 2) {
                Timer::remove($_SESSION['timer_id']);
            }
        }, [1, 2, 3], true);
        $_SESSION['timer_id'] = $timer;
    }
}
